﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_Positions : UserControl
    {
        private IPositionBLL posBLL = new PositionBLL();
        private IEmployeeTypeBLL emptypeBLL = new EmployeeTypeBLL();
        private IPositionTypeBLL postypeBLL = new PositionTypeBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
		private IExecutiveBLL execBLL = new ExecutiveBLL();
		private IClassSpecificationBLL specBLL = new ClassSpecificationBLL();
		private IClassBLL classBLL = new ClassBLL();
        private IJobSiteBLL jobsiteBLL = new JobSiteBLL();

        private PositionInfo posInfo;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            FillDataToEmpType();
            FillDataToExecPositions();
            FillDataToUpperPos(1);
            FillDataToJobSite();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    //string Seq = DataBinder.Eval(DataItem, "Sequence").ToString();
                    //IList<PositionInfo> posList = posBLL.ListPositionsBySequence(Seq);
                    //posInfo = posList[0];

                    lblPosSeq.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    cmbEmpType.SelectedValue = DataBinder.Eval(DataItem, "EmployeeType.Sequence").ToString();
                    FillDataToPositionType(Int32.Parse(cmbEmpType.SelectedValue));

                    cmbPosType.SelectedValue = DataBinder.Eval(DataItem, "PositionType.Sequence").ToString();
                    FillDataToFieldPositions(cmbPosType.SelectedValue);
                    FillDataToClassSpec(cmbPosType.SelectedValue);

                    if (DataBinder.Eval(DataItem, "Field.Sequence") != null)
                        cmbFieldPosition.SelectedValue = DataBinder.Eval(DataItem, "Field.Sequence").ToString();

                    if (DataBinder.Eval(DataItem, "Executive.Sequence") != null)
                        cmbExecPosition.SelectedValue = DataBinder.Eval(DataItem, "Executive.Sequence").ToString();

                    if (DataBinder.Eval(DataItem, "ClassSpecification.Sequence") != null)
                        //cmbClassSpec.SelectedValue = DataBinder.Eval(DataItem, "ClassSpecification.Sequence").ToString();
						cmbClass.SelectedValue = DataBinder.Eval(DataItem, "Class.Sequence").ToString();

                    if (DataBinder.Eval(DataItem, "JobSite.Sequence") != null)
                        cmbJobSite.SelectedValue = DataBinder.Eval(DataItem, "JobSite.Sequence").ToString();

                    if (DataBinder.Eval(DataItem, "Upper.Sequence") != null)
                        cmbUpperEmpPos.SelectedValue = DataBinder.Eval(DataItem, "Upper.Sequence").ToString();

                    cmbPosStatus.SelectedValue = DataBinder.Eval(DataItem, "Status.Sequence").ToString();
                    txtPosRef.Text = DataBinder.Eval(DataItem, "RefNo").ToString();
                    txtPosName.Text = DataBinder.Eval(DataItem, "Name").ToString();
                    txtDateDefined.SetValue(DataBinder.Eval(DataItem, "DefinedDate", "{0:dd/MM/yyyy}").ToString());
                    txtSalaryQuota.Text = DataBinder.Eval(DataItem, "SalaryQuota").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }
                else
                {
                    cmbPosStatus.Enabled = false;
                }
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        public void FillDataToEmpType()
        {
            IList<EmployeeTypeInfo> InfoList = emptypeBLL.ListEmployeeTypes(null);

            cmbEmpType.DataSource = InfoList;
       }

        public void FillDataToPositionType(Nullable<int> nEmpType)
        {
            IList<PositionTypeInfo> InfoList = postypeBLL.ListPositionTypes(null, nEmpType);

            cmbPosType.DataSource = InfoList;
            cmbPosType.DataBind();
        }

        public void FillDataToFieldPositions(string PosTypeSeq)
        {
            IList<FieldInfo> InfoList = fieldBLL.ListFields(PosTypeSeq, "");

            cmbFieldPosition.DataSource = InfoList;
            cmbFieldPosition.DataBind();
        }

        public void FillDataToExecPositions()
        {
            IList<ExecutiveInfo> InfoList = execBLL.ListExecutives(null, "U");

            cmbExecPosition.DataSource = InfoList;
        }

        public void FillDataToClassSpec(string PosTypeSeq)
        {
            int posType = PosTypeSeq.Length > 0 ? Int32.Parse(PosTypeSeq) : 0;
            IList<ClassSpecificationInfo> InfoList = specBLL.ListClassSpecification(null, posType);

			//cmbClassSpec.DataSource = InfoList;
			//cmbClassSpec.DataBind();
       }

		public void FillDataToClass(string PosTypeSeq)
		{
			int posType = PosTypeSeq.Length > 0 ? Int32.Parse(PosTypeSeq) : 0;
			IList<ClassInfo> InfoList = classBLL.ListClasses(PosTypeSeq);//.ListClassSpecification(null, posType);

			cmbClass.DataSource = InfoList;
			cmbClass.DataBind();
		}

        public void FillDataToUpperPos(Nullable<int> nPosSeq)
        {
            IList<PositionInfo> InfoList = posBLL.ListPositions();

            cmbUpperEmpPos.DataSource = InfoList;
        }

        protected void FillDataToJobSite()
        {
            JobSiteTypeInfo Info = new JobSiteTypeInfo();
            IList<JobSiteInfo> InfoList = jobsiteBLL.ListJobSiteByTypes("1");// .LoadListJobSite(null, Info.External);

            cmbJobSite.DataSource = InfoList;
         }

        private void BindTree(RadTreeView treeView)
        {
            JobSiteTypeInfo Info = new JobSiteTypeInfo();
            IList<JobSiteInfo> InfoList = jobsiteBLL.ListJobSiteByTypes("1");

            treeView.DataSource = InfoList; // job.SelectTreeView("", "0");
            treeView.DataBind();

        }
        protected void GetPosLineName()
        {
            IList<FieldInfo> Info = fieldBLL.ListFields(cmbFieldPosition.SelectedValue);

            txtPosName.Text = Info[0].LineName;
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
             posBLL.Update(Convert.ToInt32(lblPosSeq.Text)
                , txtPosRef.Text.Trim()
                , txtPosName.Text.Trim()
                , ""
                , txtDateDefined.GetStringValue().Length > 0 ? DateTime.Parse(txtDateDefined.GetStringValue().Trim()) : (DateTime?)null
                , (DateTime?)null
                , Convert.IsDBNull(txtSalaryQuota.Text) ? 0 : Convert.ToDouble(txtSalaryQuota.Text)
                , 0
                , 0
                , cmbFieldPosition.SelectedValue
                , cmbExecPosition.SelectedValue
                , cmbEmpType.SelectedValue
                , cmbJobSite.SelectedValue
                , cmbPosType.SelectedValue
                , cmbUpperEmpPos.SelectedValue
                , ""
                , null //, cmbClassSpec.SelectedValue
				, cmbClass.SelectedValue
                , "1" // เป็น 1 หมายถึง ว่าง   cmbPosStatus.SelectedValue
                , txtRemarks.Text.Trim()
                , 1);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }


        protected void cmbFieldPosition_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GetPosLineName();
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            posBLL.Insert(txtPosRef.Text.Trim()
                , txtPosName.Text.Trim()
                , ""
                , txtDateDefined.GetStringValue().Length > 0 ? DateTime.Parse(txtDateDefined.GetStringValue().Trim()) : (DateTime?)null
                , (DateTime?)null
                , Convert.IsDBNull(txtSalaryQuota.Text) ? 0 : Convert.ToDouble(txtSalaryQuota.Text)
                ,null
                , 0
                , cmbFieldPosition.SelectedValue
                , cmbExecPosition.SelectedValue
                , cmbEmpType.SelectedValue
                , cmbJobSite.SelectedValue
                , cmbPosType.SelectedValue
                , cmbUpperEmpPos.SelectedValue
                , null
                , null// cmbClassSpec.SelectedValue
				, cmbClass.SelectedValue
                , "1" // เป็น 1 หมายถึง ว่าง    cmbPosStatus.SelectedValue
                , txtRemarks.Text.Trim()
                , 1);
        }

        protected void cmbJobSite_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            //e.Item.Text = string.Concat(e.Item.Text.ToLower().Split(' ')[0], "@telerik.com");
        }

        protected void cmbPosType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillDataToFieldPositions(cmbPosType.SelectedValue);
            FillDataToClassSpec(cmbPosType.SelectedValue);
        }

        protected void cmbEmpType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Nullable<int> EmpType = cmbEmpType.SelectedValue.Length <= 0 ? (int?)null : Int32.Parse(cmbEmpType.SelectedValue);

            FillDataToPositionType(EmpType);            
        }

        protected void RadDatePicker1_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            //DateTime dt = RadDatePicker1.SelectedDate.Value;

            //RadDatePicker1.SelectedDate.Value = Convert.ToString(dt.Date);
        }

    }
}